package cn.hx.mall.goods.controller;

import cn.hx.mall.goods.entity.ProductAttribute;
import cn.hx.mall.goods.entity.ProductAttributeCategory;
import cn.hx.mall.goods.entity.ProductAttributeValue;
import cn.hx.mall.goods.service.ProductAttributeCategoryService;
import cn.hx.mall.goods.service.ProductAttributeService;
import cn.hx.mall.goods.service.ProductAttributeValueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("goods")
public class ProductTypeController {


    @Autowired
    private ProductAttributeCategoryService productAttributeCategoryService;

    /*type主页*/
    @GetMapping("productType")
    public String Sort(Model model,Integer page,Integer pageSize) {
        List<ProductAttributeCategory> productCategories = productAttributeCategoryService.queryAllByLimit(0, 10);
        List<ProductAttributeCategory> count = productAttributeCategoryService.queryAllByLimit(0, 1000000);
        int size = count.size();
        model.addAttribute("count", size);
        model.addAttribute("productCategoryList", productCategories);
        if (page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        PageHelper.startPage(page,pageSize);
        List<ProductAttributeCategory> l = productAttributeCategoryService.queryAll();
        PageInfo<ProductAttributeCategory> p =  new PageInfo<ProductAttributeCategory>(l,pageSize);
        model.addAttribute("productAttributeList2", p);
        return "goods/type";
    }

    @PostMapping("/saveTypeAdd")
    public String saveTypeAdd(Model model, String name) {
        ProductAttributeCategory productAttributeCategory = new ProductAttributeCategory();
        productAttributeCategory.setName(name);
        productAttributeCategoryService.insert(productAttributeCategory);
        List<ProductAttributeCategory> productCategories = productAttributeCategoryService.queryAllByLimit(0, 10);
        List<ProductAttributeCategory> count = productAttributeCategoryService.queryAllByLimit(0, 1000000);
        int size = count.size();
        model.addAttribute("count", size);
        model.addAttribute("productCategoryList", productCategories);
        return "goods/type";
    }

    @Autowired
    private ProductAttributeService productAttributeService;
    @Autowired
    private ProductAttributeValueService productAttributeValueService;

    /*属性列表*/
    /*主页*/
    @GetMapping("attributeList")
    public ModelAndView attributeList(Long id, Integer type,Integer page,Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView("goods/typeAttributeList");
        ProductAttribute productAttribute = new ProductAttribute();
        productAttribute.setProductAttributeCategoryId(id);
        productAttribute.setType(type);
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryService.queryById(id);
        modelAndView.addObject("productAttributeCategory", productAttributeCategory);
        modelAndView.addObject("type", type);
        if (pageSize==null){
            pageSize=5;
        }
        if (page==null){
            page=1;
        }
        PageHelper.startPage(page,pageSize);
        List<ProductAttribute> list = productAttributeService.queryAll(productAttribute);
        PageInfo<ProductAttribute> p =  new PageInfo<ProductAttribute>(list,pageSize);
        modelAndView.addObject("productAttributeList2", p);
        return modelAndView;
    }

    /*属性列表*/
    /*添加*/
    @GetMapping("typeAttributeList/add")
    public ModelAndView typeAttributeListAdd(Integer types, long id) {
        ModelAndView modelAndView = new ModelAndView("goods/typeAttributeListAdd");
        List<ProductAttributeCategory> productAttributeCategories = productAttributeCategoryService.queryAllByLimit(0, 100);
        modelAndView.addObject("PAC", productAttributeCategories);
        List<ProductAttributeValue> productAttributeValues = productAttributeValueService.queryAllByLimit(0, 1000);
        modelAndView.addObject("productAttributeValues", productAttributeValues);
        modelAndView.addObject("types", types);
        modelAndView.addObject("id", id);
        modelAndView.addObject("tt", 1);
        return modelAndView;
    }

    /*属性列表*/
    /*修改*/
    @GetMapping("typeAttributeList/change")
    public ModelAndView typeAttributeListChange(String per, long ids, Integer types) {
        ModelAndView modelAndView = new ModelAndView("goods/typeAttributeListAdd");
        ProductAttribute productAttribute = productAttributeService.queryById(ids);
        List<ProductAttributeCategory> productAttributeCategories = productAttributeCategoryService.queryAllByLimit(0, 100);
        modelAndView.addObject("PAC", productAttributeCategories);
        modelAndView.addObject("productAttribute", productAttribute);
        List<ProductAttributeValue> productAttributeValues = productAttributeValueService.queryAllByLimit(0, 1000);
        modelAndView.addObject("productAttributeValues", productAttributeValues);
        modelAndView.addObject("ids", ids);
        modelAndView.addObject("type", types);
        modelAndView.addObject("tt", 0);
        modelAndView.addObject("per", per);
        return modelAndView;
    }

    /*属性列表*/
    /*编辑保存*/
    @PostMapping("typeAttributeListSave")
    public ModelAndView typeAttributeListSave(Long per, Long ids, Integer type,
                                              String name,
                                              Long productAttributeCategoryId,
                                              Integer selectType,
                                              Integer inputType,
                                              String inputList,
                                              Integer sort,
                                              Integer filterType,
                                              Integer searchType,
                                              Integer relatedStatus,
                                              Integer handAddStatus) {
        ModelAndView modelAndView = new ModelAndView("goods/typeAttributeList");
        ProductAttributeCategory p = null;
        ProductAttribute ps = null;
        if (per != null ) {
            p = productAttributeCategoryService.queryById(per);
            if (type == 0) {
                ProductAttribute p0 = new ProductAttribute();
                p0.setType(type);
                p0.setProductAttributeCategoryId(productAttributeCategoryId);
                p.setAttributeCount(productAttributeService.queryAll(p0).size()+ 1);
                if (productAttributeService.queryById(ids) != null) {
                    ps = productAttributeService.queryById(ids);
                    ps.setName(name);
                    ps.setProductAttributeCategoryId(productAttributeCategoryId);
                    ps.setSelectType(selectType);
                    ps.setInputList(inputList);
                    ps.setSort(sort);
                    ps.setInputType(inputType);
                    ps.setFilterType(filterType);
                    ps.setSearchType(searchType);
                    ps.setRelatedStatus(relatedStatus);
                    ps.setHandAddStatus(handAddStatus);
                    productAttributeService.update(ps);
                } else {
                    ps = new ProductAttribute();
                    ps.setName(name);
                    ps.setProductAttributeCategoryId(productAttributeCategoryId);
                    ps.setSelectType(selectType);
                    ps.setInputList(inputList);
                    ps.setSort(sort);
                    ps.setInputType(inputType);
                    ps.setFilterType(filterType);
                    ps.setSearchType(searchType);
                    ps.setRelatedStatus(relatedStatus);
                    ps.setHandAddStatus(handAddStatus);
                    ps.setType(type);
                    productAttributeService.insert(ps);
                }
            }
            if (type == 1) {
                ProductAttribute p0 = new ProductAttribute();
                p0.setType(type);
                p0.setProductAttributeCategoryId(productAttributeCategoryId);
                p.setParamCount(productAttributeService.queryAll(p0).size()+ 1);
                if (productAttributeService.queryById(ids) != null) {
                    ps = productAttributeService.queryById(ids);
                    ps.setName(name);
                    ps.setProductAttributeCategoryId(productAttributeCategoryId);
                    ps.setSelectType(selectType);
                    ps.setInputList(inputList);
                    ps.setSort(sort);
                    productAttributeService.update(ps);
                } else {
                    ps = new ProductAttribute();
                    ps.setName(name);
                    ps.setProductAttributeCategoryId(productAttributeCategoryId);
                    ps.setSelectType(selectType);
                    ps.setInputList(inputList);
                    ps.setSort(sort);
                    ps.setInputType(inputType);
                    ps.setFilterType(filterType);
                    ps.setSearchType(searchType);
                    ps.setRelatedStatus(relatedStatus);
                    ps.setHandAddStatus(handAddStatus);
                    ps.setType(type);
                    productAttributeService.insert(ps);
                }
            }
        }
        productAttributeCategoryService.update(p);
        ProductAttribute productAttribute = new ProductAttribute();
        productAttribute.setProductAttributeCategoryId(per);
        productAttribute.setType(type);
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryService.queryById(per);
        modelAndView.addObject("productAttributeCategory", productAttributeCategory);
        PageHelper.startPage(1,5);
        List<ProductAttribute> list = productAttributeService.queryAll(productAttribute);
        PageInfo<ProductAttribute> page =  new PageInfo<ProductAttribute>(list,5);
        modelAndView.addObject("productAttributeList2", page);
        modelAndView.addObject("type", type);
        modelAndView.addObject("page",1);
        return modelAndView;
    }

    /*属性列表*/
    /*删除*/
    @PostMapping("typeAttributeList/del")
    @ResponseBody
    public Map<String,Object> typeAttributeListDel(long id) {
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        return map;
    }
    /*属性列表*/
    /*确认删除*/
    @PostMapping("typeAttributeList/dell")
    public ModelAndView dell(long id,Integer btn) {
        ModelAndView modelAndView = new ModelAndView("goods/typeAttributeList");
        ProductAttribute productAttribute1 = productAttributeService.queryById(id);
        ProductAttribute productAttribute = new ProductAttribute();
        productAttribute.setProductAttributeCategoryId(productAttribute1.getProductAttributeCategoryId());
        productAttribute.setType(productAttribute1.getType());
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryService.queryById(productAttribute1.getProductAttributeCategoryId());
        if (btn==null){
            productAttributeService.deleteById(id);
            if (productAttribute1.getType()==0){
                productAttributeCategory.setAttributeCount(productAttributeService.queryAll(productAttribute).size());
                productAttributeCategoryService.update(productAttributeCategory);
            }
            if (productAttribute1.getType()==1){
                productAttributeCategory.setParamCount(productAttributeService.queryAll(productAttribute).size());
                productAttributeCategoryService.update(productAttributeCategory);
            }
        }
        modelAndView.addObject("productAttributeCategory", productAttributeCategory);
        PageHelper.startPage(1,5);
        List<ProductAttribute> list = productAttributeService.queryAll(productAttribute);
        PageInfo<ProductAttribute> page =  new PageInfo<ProductAttribute>(list,5);
        modelAndView.addObject("productAttributeList2", page);
        modelAndView.addObject("type", productAttribute1.getType());
        modelAndView.addObject("page",1);
        return modelAndView;
    }

    @PostMapping("updateType")
    @ResponseBody
    public ProductAttributeCategory updateType(long id) {
        return productAttributeCategoryService.queryById(id);
    }

    @PostMapping("updateProduct")
    public String updateProduct(Model model, Integer i, String name) {
        ProductAttributeCategory productAttributeCategory = new ProductAttributeCategory();
        Long ii = Long.valueOf(i);
        productAttributeCategory.setId(ii);
        productAttributeCategory.setName(name);
        productAttributeCategoryService.update(productAttributeCategory);
        List<ProductAttributeCategory> productCategories = productAttributeCategoryService.queryAllByLimit(0, 10);
        List<ProductAttributeCategory> count = productAttributeCategoryService.queryAllByLimit(0, 1000000);
        int size = count.size();
        model.addAttribute("count", size);
        model.addAttribute("productCategoryList", productCategories);
        return "goods/type";
    }

    /*删除*/
    @PostMapping("del")
    @ResponseBody
    public ProductAttributeCategory delProduct(Long id) {
        return productAttributeCategoryService.queryById(id);
    }

    @PostMapping("delProduct")
    public String delProduct(Model model, String id) {
        productAttributeCategoryService.deleteById(Long.valueOf(id));
        List<ProductAttributeCategory> productCategories = productAttributeCategoryService.queryAllByLimit(0, 10);
        List<ProductAttributeCategory> count = productAttributeCategoryService.queryAllByLimit(0, 1000000);
        int size = count.size();
        model.addAttribute("count", size);
        model.addAttribute("productCategoryList", productCategories);
        return "goods/type";
    }

}
